package com.tyning.newcode.jz06;

//剑指 Offer 06. 从尾到头打印链表
//输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。
//
//
//
//示例 1：
//
//输入：head = [1,3,2]
//输出：[2,3,1]
//
//
//限制：
//
//0 <= 链表长度 <= 10000
// https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/


import java.util.ArrayList;
import java.util.List;

/**
 * 将链表弄成有序数组
 * 第二种方法使用的是栈
 *
 * @author haining
 */
public class Solution {

    public static void main(String[] args) {
        ListNode listNode = null;
        ListNode next;
        ListNode newNode = new ListNode(listNode.val);
        newNode.next = null;

    }



    static ArrayList<Integer> list = new ArrayList();

    public static void main2(String[] args) {
        Solution solution = new Solution();
        ListNode listNode = new ListNode(1);
        solution.reversePrint(listNode);
    }

    public int[] reversePrint(ListNode head) {
        if (head == null) {
            return new int[]{};
        }
        List<Integer> list = put(head);
        int[] arr = new int[list.size()];
        for(int i = 0; i < list.size();i++){
            arr[i] = list.get(i);
        }
        return arr;
    }

    public List<Integer> put(ListNode head) {
        if (head.next != null){
            put(head.next);
        }
        list.add(head.val);
        return list;
    }



//    public int[] reversePrint(ListNode head) {
//        put(head, list);
//        return list.stream().mapToInt(Integer::intValue).toArray();
//    }

    public void put(ListNode head, ArrayList list) {
        if (head.next != null) {
            put(head.next, list);
        }
        list.add(head.val);
    }

}
